var express = require('express');
var router = express.Router();
var DB = require('../lib/mysql');
var sendMail = require('../lib/sendMail');
var resBody = require('../lib/common/response');
/* GET users listing. */
router.get('/list', async (req, res, next) => {
  var resArr = await DB('select * from users where del_flg = 0');
  return res.json(resBody(true,resArr));
});

router.post('/add', async (req, res, next) => {
	var user = req.body;
	if(!(/^\w+@\w+\.\w+$/g.test(user.email))){
		return res.send({
			code: 0,
			msg: '邮箱有误'
		});
	}
	if(!!user.id){
		var resArr = await DB(`update users set user_name = '${user.user_name}',email = '${user.email}' where id= ${user.id}`);
	}else{
		var resArr = await DB(`insert into users (user_name,email) values ('${user.user_name}','${user.email}')`);
		user.id = resArr.insertId;
	}
	if(user.id){
		var userList = await DB(`select * from users where id = ${user.id}`);
	}
	return res.json(resBody(!!userList[0],user));
});


router.get('/del', async (req, res, next) => {
	var userId = req.query.id;
	var resArr = await DB(`update users set del_flg = 1 where id = ${userId}`);
	return res.json(resBody(resArr.changedRows));
});

router.get('/send', async (req, res, next) => {
	var userId = req.query.id;
	var resArr = await DB(`select * from users where id = ${userId}`);
	console.log(resArr[0].email);
	var sendStatus = await sendMail({
		to: resArr[0].email,
		html: '66666'
	});
	var resArr = await DB(`update users set interview_status = 1 where id = ${userId}`);
	return res.json(resBody(sendStatus));
});
module.exports = router;
